package com.wuliner.unlock_new

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.Manifest
import android.content.Intent
import android.util.Log
import android.widget.Button
import android.widget.EditText

class MainActivity : AppCompatActivity() {
    @SuppressLint("ResourceType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        login()


    }

    private fun login() {
        val editView1 = findViewById<InputView>(R.id.inputView1).apply {
            setAlertText("用户名")
            setEditHint("请输入用户名")
        }

        val editView2 = findViewById<InputView>(R.id.inputView2).apply {
            setAlertText("密码")
            setEditHint("请输入密码")
        }
        val switchButton = findViewById<Button>(R.id.switchButton)
        val loginButton = findViewById<Button>(R.id.button)

        switchButton.setOnClickListener {
            switchUnlock()
        }
        loginButton.setOnClickListener {
            val userName = editView1.editText.text.toString()
            val password = editView2.editText.text.toString()
            editView1.setAlertText("用户名")
            editView1.changeState(true)
            editView2.setAlertText("密码")
            editView2.changeState(true)
            val user = User.loadUserInfo(userName)

            if (password == "") {
                editView2.setAlertText("密码不能为空")
                editView2.changeState(false)
            }

            if (userName == "") {
                editView1.setAlertText("用户名不能为空")
                editView1.changeState(false)
            } else if (user == null) {
                editView1.setAlertText("用户不存在")
                editView1.changeState(false)
            } else if (password == user.numPsd) {
                editView1.setAlertText("用户名")
                editView1.changeState(true)
                editView1.setAlertText("用户不存在")
                editView1.changeState(true)
                switchActivity()
            }

        }

    }

    //跳转界面到图案解锁界面
    private fun switchUnlock() {
        TODO("Not yet implemented")
    }

    //登录成功跳转界面
    private fun switchActivity() {
        startActivity(Intent(this, SuccessActivity::class.java))
    }


    //伴生Context
    companion object {
        lateinit var appContext: Context
    }

}


